<?php
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 akademik_master_tingkat
	 akademik_master_tingkat_add
	 akademik_master_tingkat_edit
	 akademik_master_tingkat_del	 
	 */
	class TingkatController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";		
		public function init() {
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");				
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_master_tingkat");
			$this->subMenu = "Tingkat";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"");			
			$this->render('index',$data);
		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			if ($onlyActive){
				$mTingkat = MTingkatan::model()->findAll(array("condition"=>"is_active = TRUE and department_id = '$department_id'","order"=>"id"));
			}else{
				$mTingkat = MTingkatan::model()->findAll(array("condition"=>"department_id = '$department_id'","order"=>"id"));
			}			
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mTingkat;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_tingkat_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$tingkat = new MTingkatan;
				$tingkat->department_id = Yii::app()->request->getParam("department_id","");
				$tingkat->pos = Yii::app()->request->getParam("pos",0);
				$tingkat->name = Yii::app()->request->getParam("name","");
				$tingkat->description = Yii::app()->request->getParam("description","");
				$tingkat->is_active=$status;
				if ($tingkat->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($tingkat->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_master_tingkat_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$tingkat = MTingkatan::model()->find("id=:id",array(":id"=>$id));
				$tingkat->pos = Yii::app()->request->getParam("pos",0);
				$tingkat->name = Yii::app()->request->getParam("name","");
				$tingkat->description = Yii::app()->request->getParam("description","");
				$tingkat->is_active=$status;
				if ($tingkat->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($tingkat->getErrors());
				}				
			}else if ($op == "del"){
				if (!LoginUser::getPermission("akademik_master_tingkat_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_tingkatan where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","description"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_tingkat_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
				$data["pos"] = CMTingkatan::getNewPos($department_id);
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_master_tingkat_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;								
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
				$rsTingkat = MTingkatan::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
					$data["department_id"] = $rsTingkat[0]->department_id;
					$data["pos"] = $rsTingkat[0]->pos;
					$data["name"] = $rsTingkat[0]->name;
					$data["description"] = $rsTingkat[0]->description;
					$data["is_active"] = $rsTingkat[0]->is_active;
					
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>